/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 */

package com.device.idea.demo.core;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * CommonAdapter
 *
 * @since 2020-12-12
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {
    /**
     * Context
     */
    protected Context mContext;

    /**
     * LayoutInflater
     */
    protected LayoutInflater mInflater;

    /**
     * List
     */
    protected List<T> mList;

    private int mLayoutId;

    private OnItemClickListener<T> mClickListener;

    private OnItemLongClickListener<T> mLongClickListener;

    /**
     * CommonAdapter
     *
     * @param context context
     * @param list List
     * @param layoutId layoutId
     */
    public CommonAdapter(Context context, List<T> list, int layoutId) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mList = list;
        mLayoutId = layoutId;
    }

    /**
     * onCreateViewHolder
     *
     * @param parent ViewGroup
     * @param viewType viewType
     * @return CommonViewHolder
     */
    @NonNull
    @Override
    public CommonViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(mLayoutId, parent, false);
        return new CommonViewHolder(mContext, itemView);
    }

    /**
     * onBindViewHolder
     *
     * @param holder CommonViewHolder
     * @param position position
     */
    @Override
    public void onBindViewHolder(@NonNull CommonViewHolder holder, int position) {
        T itemData = mList.get(position);
        holder.itemView.setOnClickListener(view -> {
            if (mClickListener != null) {
                mClickListener.onClick(view, itemData, position);
            }
        });
        holder.itemView.setOnLongClickListener(view -> {
            if (mLongClickListener != null) {
                mLongClickListener.onLongClick(itemData, position);
                return true;
            }
            return false;
        });
        convert(holder, itemData, position);
    }

    /**
     * convert
     *
     * @param holder CommonViewHolder
     * @param item item
     * @param position position
     */
    public abstract void convert(CommonViewHolder holder, T item, int position);

    /**
     * getItemCount
     *
     * @return list size
     */
    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * setOnItemClickListener
     *
     * @param clickListener OnItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener<T> clickListener) {
        mClickListener = clickListener;
    }

    /**
     * OnItemClickListener
     *
     * @param <T> CommonViewHolder
     * @since 2020-01-01
     */
    public interface OnItemClickListener<T> {
        /**
         * click callback method
         *
         * @param view Click View
         * @param itemData item data
         * @param position item position
         */
        void onClick(View view, T itemData, int position);
    }

    /**
     * OnItemLongClickListener
     *
     * @param <T> CommonViewHolder
     * @since 2020-01-01
     */
    public interface OnItemLongClickListener<T> {
        /**
         * long click callback method
         *
         * @param itemData item data
         * @param position item position
         */
        void onLongClick(T itemData, int position);
    }
}